//
//  FavoritesViewController.m
//  Alfresco
//
//  Created by Michael Muller on 10/7/09.
//  Copyright 2010 Zia Consulting.  Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
//

#import "FavoritesViewController.h"
#import "SavedDocument.h"
#import "DocumentViewController.h"
#import "RepositoryItemTableViewCell.h"
#import "Utility.h"

@implementation FavoritesViewController

@synthesize favorites;

- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Favorites";
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [favorites count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	RepositoryItemTableViewCell *cell = (RepositoryItemTableViewCell *) [tableView dequeueReusableCellWithIdentifier:RepositoryItemCellIdentifier];
    if (cell == nil) {
		NSArray *nibItems = [[NSBundle mainBundle] loadNibNamed:@"RepositoryItemTableViewCell" owner:self options:nil];
		cell = [nibItems objectAtIndex:0];
		NSAssert(nibItems, @"Failed to load object from NIB");
    }
    	
	cell.filename.text = [favorites objectAtIndex:[indexPath row]];
	cell.details.text = [SavedDocument sizeOfSavedFile:cell.filename.text];
	cell.image.image = imageForFilename(cell.filename.text);
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	DocumentViewController *doc = [[DocumentViewController alloc] initWithNibName:@"DocumentViewController" bundle:[NSBundle mainBundle]];
	
	doc.fileName = [favorites objectAtIndex:[indexPath row]];
	doc.fileData = [NSData dataWithContentsOfFile:[SavedDocument pathToSavedFile:doc.fileName]];
	
	doc.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:doc animated:YES];
	
	[doc release];
}

- (void)viewWillAppear:(BOOL)animated {
	[favorites release];
	favorites = [SavedDocument list];
	[(UITableView *) self.view reloadData];
}

- (void)dealloc {
    [super dealloc];
}


@end

